﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace ConsoleApplication27
{
    public static class ParallelUtil
    {
        private static IEnumerable<bool> Infinite()
        {
            while (true) yield return true;
        }

        private static ParallelOptions NoParallelOptions = new ParallelOptions();

        public static void InParallelWhile(this Action<ParallelLoopState> action, Func<bool> condition, ParallelOptions options = null)
        {
            if (options == null) options = NoParallelOptions;

            Parallel.ForEach(IterateForever(), options,
                (ignored, loopState) =>
                {
                    if (!condition())
                    {
                        loopState.Stop();
                    }
                    else
                    {
                        action(loopState);
                    }
                });

        }

        private static IEnumerable<bool> IterateForever()
        {
            while (true)
            {
                yield return true;
            }
        }
    }

   



}
